package com.netcracker.studportal.server.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.netcracker.studportal.server.dao.FacultyDAO;
import com.netcracker.studportal.server.model.Faculty;
import com.netcracker.studportal.server.model.Professor;
import com.netcracker.studportal.server.util.HibernateUtil;

public class FacultyDAOImpl implements FacultyDAO {
	private static final Logger LOG = Logger.getLogger(FacultyDAOImpl.class);

	@Override
	public void Add(Faculty fs) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.save(fs);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public void Update(Faculty fs) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.update(fs);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public Faculty getById(Long id) throws Exception {
		Session session = null;
		Faculty fs = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			fs = (Faculty) session.load(Faculty.class, id);
			Hibernate.initialize(fs.getUniversity());
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();

			}
		}
		return fs;
	}

	@Override
	public void Delete(Faculty fs) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(fs);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public Collection<Faculty> getAll() throws Exception {
		Session session = null;
		List<Faculty> faculties = new ArrayList<Faculty>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			faculties = session.createCriteria(Faculty.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return faculties;
	}
	
	@Override
	public Collection<Faculty> getFacultiesByUniversity(Long univ_id)
			throws Exception {
		Session session = null;
		List<Faculty> list;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			list = (List<Faculty>) session
					.createSQLQuery(
							"select * from faculties where univ_id=" + univ_id)
					.addEntity(Faculty.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return list;
	}
	
	/**
	 * Realization of singleton.
	 */
	private FacultyDAOImpl() {}
	private static FacultyDAO instance;
	public static FacultyDAO getInstance() {
		if (instance == null) {
			instance = new FacultyDAOImpl();
		}
		return instance;
	}

}
